//
//  HMMessageViewController.m
//  HowToDo
//
//  Created by qu on 2017/1/7.
//  Copyright © 2017年 KY. All rights reserved.
//

#import "HMMessageViewController.h"
#import <SVProgressHUD/SVProgressHUD.h>
@interface HMMessageViewController ()<UITextViewDelegate,UITextInputTraits>

@property (nonatomic, strong) UITextView *messageTextView;
@property (nonatomic, strong) UILabel *promptLabel;
@property (nonatomic, strong) UILabel *phLabel;

@end

@implementation HMMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //线
    UIView *lineView = [[UIView alloc]init];
    [self.view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.equalTo(self.view);
        make.top.offset(64);
        make.height.offset(0.5);
    }];
    lineView.backgroundColor = [UIColor colorWithHex:0x808080];
    //取消按钮
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:cancelBtn];
    
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.view).offset(17);
        make.bottom.equalTo(lineView.mas_top);
        
    }];
    [cancelBtn addTarget:self action:@selector(goBackSpringFlowers) forControlEvents:UIControlEventTouchUpInside];
    [cancelBtn setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
    cancelBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    //标题
    UILabel *titleLabel = [[UILabel alloc]init];
    
    [self.view addSubview:titleLabel];
    
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        [make centerX];
        make.bottom.equalTo(lineView.mas_top).offset(-8);
    }];
    titleLabel.text = @"写留言";
    titleLabel.font = [UIFont fontWithName:@ "Arial Rounded MT Bold"  size:(20.0)];
    //提交按钮
    UIButton *presentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:presentBtn];
    
    [presentBtn setTitle:@"提交" forState:UIControlStateNormal];
    
    [presentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.view).offset(-17);
        make.bottom.equalTo(lineView.mas_top);
        
    }];
    [presentBtn addTarget:self action:@selector(presentBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [presentBtn setTitleColor:[UIColor colorWithHex:0xff8000] forState:UIControlStateNormal];
    presentBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    //输入
    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectZero];
    
    [self.view addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.equalTo(self.view);
        make.top.equalTo(lineView.mas_bottom);
        make.height.offset(250);
        
    }];
    
    textView.delegate = self;
    
    self.messageTextView = textView;
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 10;// 字体的行间距
    
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:15],
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 };
    textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
    [self.messageTextView addSubview:self.phLabel];
    
    [self.phLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.left.equalTo(self.messageTextView).offset(5);
        
    }];
    
    
    //尾标题
    UILabel *promptLabel = [[UILabel alloc]init];
    [self.view addSubview:promptLabel];
    [promptLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.offset(-4);
        make.top.equalTo(self.messageTextView.mas_bottom);
        
    }];
    promptLabel.text = [NSString stringWithFormat:@"剩余%zd字",2000-self.messageTextView.text.length];
    self.promptLabel.textAlignment = NSTextAlignmentCenter;
    promptLabel.textColor = [UIColor colorWithHex:0x929292];
    promptLabel.font = [UIFont systemFontOfSize:13];
    self.promptLabel = promptLabel;
    
}

- (void)textViewDidChange:(UITextView *)textView {
    
    self.promptLabel.text = [NSString stringWithFormat:@"剩余%zd字",2000-textView.text.length];
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    self.phLabel.alpha = 0;//开始编辑时
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{//将要停止编辑(不是第一响应者时)
    if (textView.text.length == 0) {
        self.phLabel.alpha = 1;
    }
    return YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [self.messageTextView resignFirstResponder];
    
}

- (void)presentBtnClick:(UIButton *)send {
    
    if (self.messageTextView.text.length<10) {
        
        [SVProgressHUD showInfoWithStatus:@"请输入不少于10字的评论!"];
        [SVProgressHUD setMinimumDismissTimeInterval:0.5];
    } else {
        
        [SVProgressHUD showSuccessWithStatus:@"提交成功!"];
        [SVProgressHUD setMinimumDismissTimeInterval:0.5];
        [self goBackSpringFlowers];
    }
}

- (void)goBackSpringFlowers {
    
    [self dismissViewControllerAnimated:YES completion:nil];

}


- (UILabel *)phLabel {
    if (!_phLabel) {
      _phLabel = [[UILabel alloc] init];
        // 文字自动换行
        _phLabel.numberOfLines = 0;
        _phLabel.text = @"欢迎参与交流留言将由作者筛选后公开显示";
        _phLabel.font = [UIFont systemFontOfSize:14];
        _phLabel.textColor = [UIColor colorWithHex:0xededed];
        [self.messageTextView addSubview:_phLabel];
    
    }
    
    return _phLabel;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
